import sys
import os

sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from config import dws_bucket
from aws_model.athena_submit import submit
from sql_lambda.delete import delete_data
from base_class.inspect_logging import print_to_log
import datetime
import json
from aws_model.aws_stepfunc import send_task_success, send_task_failure


def fridge_status_summary_m(event):
    date = event.get('p_date')
    if not date:
        date = datetime.datetime.today().strftime('%Y-%m-%d')
    table_name = sys._getframe().f_code.co_name
    delete_data(dws_bucket,table_name, date)
    sql = f'''insert into dws.fridge_status_summary_m
                (
                select
                    sn,
                    model as model,
                    if(sum(if(isNew = true,1,0))>0,true,false) as is_new,
                    count(1) report,
                    max(timeStamp) last_report_time,
                    count(distinct powerontime) poweron_times,
                    count_if(frequency = 1) frequency_1,
                    count_if(freezeDoor = true)   freeze_door_open,
                    count_if(freezeDoorB = true ) freeze_door_b_open,
                    count_if(freezeDoorA = true ) freeze_door_a_open,
                    count_if(freezeHeat = true ) freeze_heat_on,
                    count_if(freezeRoomFanStatus = true ) freeze_fan_on,
                    count_if(freezeroomfanState = true ) freeze_fan_work,
                    count_if(coldDoor = true ) cold_door_on,
                    count_if(coldRoomAirDoorHeat = true ) cold_air_door_heat_on,
                    count_if(coldRoomAirDoorStatus = true ) cold_air_door_open,
                    count_if(coldRoomFanStatus = true ) cold_fan_on,
                    count_if(coldroomfanState = true ) cold_fan_work,
                    count_if(changingDoor = true ) changing_door_open,
                    count_if(changingRoomAirDoorStatus = true ) changing_air_door_open,
                    count_if(changingRoomFanStatus = true ) changing_fan_on,
                    count_if(changingfanState = true ) changing_fan_work,
                    count_if(changingfanHeat = true ) changing_fan_heat_on,
                    count_if(changingHeat = true ) changing_heat_on,
                    count_if(condensationHeat = true ) condensation_heat_on,
                    count_if(powerRemind = true ) power_remind_on,
                    count_if(infraredSwitch = true ) infrared_switch_on,
                    count_if(compressorStatus = true ) compressor_on,
                    count_if(displayBoardStatus = true ) display_board_1,
                    count_if(wifiSwitch = true ) wifi_switch_1,
                    count_if(maintenanceEnd = true ) maintenance_end,
                    count_if(condenserFanStatus = true ) condenser_fan_on,
                    count_if(condensefanState = true ) condense_fan_work,
                    count_if(commandNull = true ) command_null_no_effect	,
                    count_if(ldSpeed = true ) ld_speed_on,
                    count_if(uvlightStatus = true ) uvlight_status_on,
                    count_if(ultraviolet = true ) ultraviolet_on,
                    count_if(temperatureCompensationHeat = true ) temperature_compensation_heat_on	,
                    count_if(electricSwitch1 = true ) electric_switch1_on,
                    count_if(electricSwitch2 = true ) electric_switch2_on,
                    count_if(electroMagnetic1 = true ) electro_magnetic1_on	,
                    count_if(electroMagnetic2 = true ) electro_magnetic2_on	,
                    count_if(coldOff = true ) cold_off_on,
                    count_if(smart = true ) smart_on,
                    count_if(fastFreeze = true ) fast_freeze_on,
                    count_if(leco = true ) leco_on,
                    count_if(holiDay = true ) holi_day_on,
                    count_if(denba = true ) denba_on,
                    count_if(childrenLock = true ) children_lock_on,
                    count_if(fastCold = true ) fast_cold_on,
                    count_if(bwOff = true ) bw_off_on,
                    count_if(iced = true ) iced_on,
                    count_if(gardenStuff = true ) garden_stuff_on,
                    count_if(faults01 = true ) faults01,
                    count_if(faults02 = true ) faults02,
                    count_if(faults03 = true ) faults03,
                    count_if(faults04 = true ) faults04,
                    count_if(faults05 = true ) faults05,
                    count_if(faults06 = true ) faults06,
                    count_if(faultp01 = true ) faultp01,
                    count_if(faultp02 = true ) faultp02,
                    count_if(faultf02 = true ) faultf02,
                    count_if(faultf03 = true ) faultf03,
                    count_if(faultf04 = true ) faultf04,
                    count_if(faultf05 = true ) faultf05,
                    count_if(faultd01 = true ) faultd01,
                    count_if(faultd02 = true ) faultd02,
                    count_if(faultd03 = true ) faultd03,
                    count_if(faultd04 = true ) faultd04,
                    substr(p_date,1,7) as p_date
                from
                    ods.iot_fridge
                where
                    date(p_date) >= (date('{date}') - interval '1' month)
                and
                    date(p_date) < date('{date}')
                group by
                    sn,model,substr(p_date,1,7)
                ) '''

    athena_response = submit(sql)
    print_to_log("athena_response: ", athena_response)
    output = {"msg": f"{table_name} submit ok", "Payload": {}}
    output = json.dumps(output)
    step_response = send_task_success(tasktoken, output)
    print_to_log("step_response: ", step_response)


if __name__ == '__main__':
    try:
        event = json.loads(sys.argv[1])
        tasktoken = event.get('TaskToken')
        fridge_status_summary_m(event)
    except Exception as e:
        print_to_log("athena_submit 失败")
        print_to_log("原因： ", str(e))
        send_task_failure(str(e), f'执行{sys._getframe().f_code.co_filename}错误', tasktoken)

